package com.example.datatest3.controller;

import com.example.datatest3.dao.UserDao;
import com.example.datatest3.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserDao userDao;

    @GetMapping("/list")
    public List<User> list(){
        return this.userDao.findAll();
    }

    @GetMapping("/user/{id}")
    public User findByid(@PathVariable int id){
        return this.userDao.findById(id).get();
    }

    @GetMapping("/add")
    public String add(){
        this.userDao.saveAndFlush(new User(3,"王五",2,2));
        return "添加成功";
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable int id){
        this.userDao.deleteById(id);
        return "删除成功";
    }

}
